package	com.mes.web.controller.llm;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONObject;

import com.google.gson.Gson;
import com.mes.entity.llm.Clazz;
import com.mes.entity.tdd.Staff;
import com.mes.service.jyy.ReportService;
import com.mes.service.llm.ClazzService;
import com.mes.utils.ResponseUtil;
/**
 *
 *@ClassName: ClazzController
 *@Description: 科目表
 *@Author llm
 *@Date 2019-05-29 11:57:41
 *@version 1.0
*/
@Controller
@RequestMapping("/Clazz")
public class ClazzController{
	@Resource ClazzService ClazzService;
	@Resource ReportService reportService;
	/** 
	 * 进入页面
	 * @param model
	 * @return 页面路径
	*/
	@RequestMapping("/page")
	public String pageClazz (Model model) {
		return "/llm/clazz";
	}
	/** 
	 * 根据主键id查询实体
	 * @param id 主键id
	 * @return 实体
	*/
	@RequestMapping("/queryById")
	@ResponseBody
	public void queryById(Long id, HttpServletResponse response, HttpSession session) {
		
		Clazz clazz = new Clazz();
		Staff staff = (Staff) session.getAttribute("staff");
		if(staff != null) {
			Long staffid = staff.getStaffid();
			
			//根据员工id查询，该员工账套的英文名称
			String ename = reportService.selectBooksEnameBysid(staffid);
			
			clazz.setTablename("jyy_books_clazz_"+ename);
		}
		clazz.setId(id);
		Clazz param = ClazzService.queryById(clazz);
		Gson gson = new Gson();
	    String json = gson.toJson(param);
	    try {
			ResponseUtil.write(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/** 
	 * 条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	@RequestMapping("/queryAllByMution")
	@ResponseBody
	public List<Clazz> queryAllByMution(Clazz param) {
		List<Clazz> list = ClazzService.queryAllByMution(param);
		return list;
	}
	/** 
	 * 条件分页查询
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/list")
	public void list(Clazz param,HttpServletResponse response, HttpSession session){
		Staff staff = (Staff) session.getAttribute("staff");
		if(staff != null) {
			Long staffid = staff.getStaffid();
			
			//根据员工id查询，该员工账套的英文名称
			String ename = reportService.selectBooksEnameBysid(staffid);
			
			param.setTablename("jyy_books_clazz_"+ename);
		}
		
		
		/*
		 * 条件分页查询内容
		 */
		List<Clazz> list = ClazzService.findPageByMution(param);
		/*
		 * 条件查询总条数
		 */
		Integer total = ClazzService.selectCountByMution(param);
		
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("total", total);
		jsonObject.put("rows", list);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/insert")
	public void insert(Clazz param, HttpServletResponse response, HttpSession session){
		
		Staff staff = (Staff) session.getAttribute("staff");
		if(staff != null) {
			Long staffid = staff.getStaffid();
			
			//根据员工id查询，该员工账套的英文名称
			String ename = reportService.selectBooksEnameBysid(staffid);
			
			param.setTablename("jyy_books_clazz_"+ename);
		}
		
		Long id = ClazzService.insert(param);
		try {
			response.getWriter().print(id);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/** 
	 * 根据主键更新 返回影响行数
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/update")
	public void update(Clazz param,HttpServletResponse response, HttpSession session){
		
		Staff staff = (Staff) session.getAttribute("staff");
		if(staff != null) {
			Long staffid = staff.getStaffid();
			
			//根据员工id查询，该员工账套的英文名称
			String ename = reportService.selectBooksEnameBysid(staffid);
			
			param.setTablename("jyy_books_clazz_"+ename);
		}
		
		Integer count = ClazzService.update(param);
		try {
			response.getWriter().print(count);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param ids 主键拼接的字符串
	 * @return 影响行数
	*/
	@RequestMapping("/delete")
	public void delete(String ids,HttpServletResponse response, HttpSession session){
		
		Clazz param = new Clazz();
		
		Staff staff = (Staff) session.getAttribute("staff");
		if(staff != null) {
			Long staffid = staff.getStaffid();
			
			//根据员工id查询，该员工账套的英文名称
			String ename = reportService.selectBooksEnameBysid(staffid);
			
			param.setTablename("jyy_books_clazz_"+ename);
		}
		param.setIds(ids);
		Integer count = ClazzService.delete(param);
		try {
			response.getWriter().print(count);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
